package com.api.shopapi.controller;


import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.TrainClassDTO;
import com.api.shopapi.entity.dto.TrainRecordDTO;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("TrainRecord")
public class TrainRecordController extends BaseController{

    @Autowired
    @Qualifier("TrainRecordServiceImplOne")
    private TrainRecordService trainRecordService;


    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;


    @Autowired
    @Qualifier("TrainServiceImplOne")
    private TrainService trainService;



    //添加
    @RequestMapping(value = "Add",method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String trainCode = jsonObject.getString("trainCode");
            String startTime = jsonObject.getString("startTime");
            String endTime = jsonObject.getString("endTime");

            String userCode = jsonObject.getString("userCode");


            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ArticleClass/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            User user = userService.getByUserCode(userCode);

            TrainRecord trainRecord = new TrainRecord();
            trainRecord.setTrainRecordCode(UUID.randomUUID().toString());
            trainRecord.setTrainCode(trainCode);
            trainRecord.setCreateTime(new Date());
            trainRecord.setStartTime(sdfYMDHMS.parse(startTime));
            trainRecord.setEndTime(sdfYMDHMS.parse(endTime));
            trainRecord.setUserCode(userCode);
            trainRecord.setGovernmentCode(user.getGovernmentCode());
            trainRecord.setInstitutionCode(user.getInstitutionCode());
            trainRecordService.insert(trainRecord);

            Object data = new Object[] { };
            return success(data);

        }catch (Exception e){
            insertError("/ArticleClass/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "GovPageApp",method = RequestMethod.POST)
    public MyResult GovPageApp(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            int pageIndex = jsonObject.getIntValue("pageIndex");
            int pageSize = jsonObject.getIntValue("pageSize");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ArticleClass/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            User user = userService.getByUserCode(userCode);

            PageInfo<TrainRecord> pageInfo =
                    trainRecordService.getPageList(pageIndex, pageSize, user.getGovernmentCode(),
                            null,null);

            List<TrainRecordDTO> listDTO = new ArrayList<>();
            for(TrainRecord trainRecord:pageInfo.getList()){
                TrainRecordDTO trainRecordDTO = mapper.map(trainRecord,TrainRecordDTO.class);
                Train train = trainService.getByTrainCode(trainRecordDTO.getTrainCode());
                trainRecordDTO.setTrainTitle(train.getTitle());
                listDTO.add(trainRecordDTO);
            }
            pageInfo.setList(Collections.unmodifiableList(listDTO));

            return success(pageInfo);

        }catch (Exception e){
            insertError("/ArticleClass/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }




}
